package com.chinairi.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chinairi.common.annotation.DoPage;
import com.chinairi.common.utils.IdGenerator;
import com.chinairi.common.utils.ResponseUtil;
import com.chinairi.common.utils.UserSession;
import com.chinairi.mall.controller.viewobject.ReplaceInfoParam;
import com.chinairi.mall.controller.viewobject.SuggestionPlanParam;
import com.chinairi.mall.dao.SuggestionProgramMapper;
import com.chinairi.mall.model.ReqModelDetail;
import com.chinairi.mall.dao.ReqModelDetailMapper;
import com.chinairi.mall.model.SuggestionProgram;
import com.chinairi.mall.service.IReqModelDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.ws.Response;
import java.util.*;

/**
 * <p>
 * 模型明细业务逻辑层接口
 * </p>
 *
 * @author 李昌赐
 * @since 2020-08-19
 */
@Service
public class ReqModelDetailServiceImpl extends ServiceImpl<ReqModelDetailMapper, ReqModelDetail> implements IReqModelDetailService {

    @Autowired
    private ReqModelDetailMapper reqModelDetailMapper;

    @Autowired
    private SuggestionProgramMapper suggestionProgramMapper;

    @Override
    @DoPage
    public List<Map<String, Object>> pageReqModelDetail(ReqModelDetail reqModelDetail) {
        return reqModelDetailMapper.pageReqModelDetail(reqModelDetail);
    }

    @Override
    public Map<String, Object> saveReqModelDetail(ReqModelDetail reqModelDetail) {
        try {
            SuggestionProgram suggestionProgram=new SuggestionProgram();
            String id=IdGenerator.UUID();
            reqModelDetail.setModelDetailId(id);
            reqModelDetail.setCreateUser(UserSession.getUserId());
            reqModelDetail.setCreateTime(new Date());
            reqModelDetail.setModelOrder(reqModelDetailMapper.getMatModelOrder(reqModelDetail.getModelId()));
            reqModelDetailMapper.insert(reqModelDetail);

            suggestionProgram.setSuggestionId(IdGenerator.UUID());
            suggestionProgram.setModelId(reqModelDetail.getModelId());
            suggestionProgram.setModelDetailId(id);
            suggestionProgram.setMatCode(reqModelDetail.getModelMatCode());
            suggestionProgram.setMatName(reqModelDetail.getModelMatName());
            suggestionProgram.setSpecification(reqModelDetail.getSpecification());
            suggestionProgram.setUnit(reqModelDetail.getModelMeasureUnit());
            suggestionProgram.setPurCycle(reqModelDetail.getPurCycle());

//            suggestionProgram.setStockNum();
//            suggestionProgram.setRemark("");
//            suggestionProgram.setConsumDate(new Date());
//            suggestionProgram.setStorageAddress("");

        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.toWarningMessage("保存失败", false);
        }
        return ResponseUtil.toSuccessMessage("保存成功", reqModelDetail);
    }

    @Override
    public Map<String, Object> getReqModelDetail(String modelDetailId) {
        return reqModelDetailMapper.getReqModelDetail(modelDetailId);
    }

    @Override
    public Map<String, Object> updateReqModelDetail(ReqModelDetail reqModelDetail) {
        try {
            reqModelDetail.setUpdateUser(UserSession.getUserId());
            reqModelDetail.setUpdateTime(new Date());
            reqModelDetailMapper.updateById(reqModelDetail);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.toWarningMessage("保存失败", false);
        }
        return ResponseUtil.toSuccessMessage("保存成功", reqModelDetail);
    }

    @Override
    public Map<String, Object> deleteReqModelDetail(String modelDetailId) {
        if (StringUtils.isEmpty(modelDetailId)) {
            return ResponseUtil.toWarningMessage("删除的信息不存在", false);
        }
        try {
            reqModelDetailMapper.deleteById(modelDetailId);
        } catch (Exception e) {
            e.printStackTrace();
           return ResponseUtil.toWarningMessage("删除失败", false);
        }
        return ResponseUtil.toSuccessMessage("删除成功", true);

    }

    @Override
    public List<Map<String, Object>> listWellNumber() {
        return reqModelDetailMapper.listWellNumber();
    }

    @Override
    public List<Map<String, Object>> listWellClassification() {
        return reqModelDetailMapper.listWellClassification();
    }

    @Override
    public List<Map<String, Object>> listWellType() {
        return reqModelDetailMapper.listWellType();
    }

    @Override
    public List<Map<String, Object>> listWorkArea() {
        return reqModelDetailMapper.listWorkArea();
    }

    @Override
    public List<Map<String, Object>> getReplaceInfo(ReplaceInfoParam replaceInfoParam) {
        List<String> StorageList=reqModelDetailMapper.getStorageName();
        int num= new Random().nextInt(3);
        List<Map<String,Object>> replaceInfoList=new ArrayList<>();
        Map<String,Object> map=new HashMap<>();
        String stockAddress=StorageList.get(num);
        map.put("matName",replaceInfoParam.getMatName());
        map.put("matCode",replaceInfoParam.getMatCode());
        map.put("specification",replaceInfoParam.getSpecification());
        map.put("reqNum",replaceInfoParam.getReqNum());
        map.put("unit",replaceInfoParam.getUnit());
        map.put("stockNum",replaceInfoParam.getStockNum());
        map.put("stockAddress",stockAddress);
        replaceInfoList.add(map);
        List<Map<String,Object>> childList=new ArrayList<>();
        for(int i=0;i<=num;i++){
            Map<String,Object> map1=new HashMap<>();
            String code=null;
            String guige=null;
            int index=new Random().nextInt(90)+10;
            if(replaceInfoParam.getMatCode().length()>2){
                 code=replaceInfoParam.getMatCode().substring(0,replaceInfoParam.getMatCode().length()-2)+index;
            }else{
                 code=String.valueOf(index);
            }
            if(replaceInfoParam.getSpecification().length()>2){
                 guige=replaceInfoParam.getSpecification().substring(0,replaceInfoParam.getSpecification().length()-2)+index;
            }else{
                 guige=String.valueOf(index);
            }
            int stoNum=new Random().nextInt(20)+(Integer.valueOf(replaceInfoParam.getReqNum())-Integer.valueOf(replaceInfoParam.getStockNum()))/2;
            String addresss=StorageList.get(i);
            map1.put("matName",replaceInfoParam.getMatName());
            map1.put("matCode",code);
            map1.put("specification",guige);
            map1.put("unit",replaceInfoParam.getUnit());
            map1.put("stockNum",stoNum);
            map1.put("stockAddress",addresss);
            childList.add(map1);
        }
        Map<String,Object> map2=new HashMap<>();
        map2.put("child",childList);
        replaceInfoList.add(map2);
        return replaceInfoList;
    }

    @Override
    public List<Map<String, Object>> getConNum(String conNum) {
        return reqModelDetailMapper.getConNum(conNum);
    }

    @Override
    public List<Map<String, Object>> getSuggestionPlanInfo(SuggestionPlanParam suggestionPlanParam) {
        return reqModelDetailMapper.getSuggestionPlanInfo(suggestionPlanParam);
    }
}
